/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wiView.graphics.UI.UIComponent;

import java.awt.EventQueue;
import java.net.URL;
import java.text.DecimalFormat;
import javax.swing.GrayFilter;
import javax.swing.ImageIcon;
import javax.swing.JSlider;

/**
 *
 * @author Hoang
 */
public class MediaControlBoardPanel extends javax.swing.JPanel {
    IMediaPanelContainer container;
    boolean isRunning = false;
    boolean isplay = false;
    private long currTime = 0;
    private long totalTime = 1;
    private float currSpeed = 1;
    boolean isUserSettingSlider = false;
    private DecimalFormat df = new DecimalFormat("0.00");

    public long getCurrTime() {
        return currTime;
    }
    public float getSpeed(){
        return this.currSpeed;
    }
    public void setSpeed(float speed){
        this.currSpeed = speed;
        lbCurSpeed.setText(String.valueOf(df.format(currSpeed)));
    }

    public void setCurrTime(long currTime) {
        this.currTime = currTime;
        if (!isUserSettingSlider) {
            if (totalTime != 0) {
                jSlider1.setValue((int) currTime);
                jCurrentTimeLabel.setText(getTimeFormat(currTime));
            }
        }
    }

    public long getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(long totalTime) {
        this.totalTime = totalTime;
        jSlider1.setMaximum((int)totalTime);
        jEndTimeLabel.setText(getTimeFormat(totalTime));
        setCurrTime(0);
    }
    
    String getTimeFormat(long time){
        String minute,second,miliSecond;
        long seconds = time/1000;
        long miliSeconds = time - seconds*1000;
        long minutes = seconds/60;
        seconds =seconds-minutes*60;
        
        if(minutes<10){
            minute = "0"+String.valueOf(minutes);
        }else
            minute = String.valueOf(minutes);
        if(seconds<10){
            second = "0"+String.valueOf(seconds);
        }else
            second = String.valueOf(seconds);
        if(miliSeconds<10){
            miliSecond = "00"+String.valueOf(miliSeconds);
        }else if(10<=miliSeconds&&miliSeconds<100)
            miliSecond = "0"+String.valueOf(miliSeconds);
              else
            miliSecond = String.valueOf(miliSeconds);
        return(minute+":"+second+":"+miliSecond);
    }
    
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    URL urlPlay = classLoader.getResource("icon/play.png");

    
    URL urlBack = classLoader.getResource("icon/back.png");
    URL urlForward = classLoader.getResource("icon/for.png");
    
    URL urlStop = classLoader.getResource("icon/stop.png");

    URL urlPause = classLoader.getResource("icon/pause.png");

    
    public void setContainer(IMediaPanelContainer con){
        container = con;
    }
    /**
     * Creates new form MediaControlBoardPanel
     */
    public MediaControlBoardPanel() {
        initComponents();
        initIconButton();
    }
    public void reset(){
        
    }
void initIconButton()
{
    jButtonPlay.setIcon(new javax.swing.ImageIcon(urlPlay));
    
    jButtonBackward.setIcon(new javax.swing.ImageIcon(urlBack));
    jButtonForward.setIcon(new javax.swing.ImageIcon(urlForward));
    
    jButtonStop.setIcon(new javax.swing.ImageIcon(urlStop));
    jButtonStop.setPressedIcon(new javax.swing.ImageIcon(GrayFilter.createDisabledImage(new javax.swing.ImageIcon(urlStop).getImage())));
}
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton4 = new javax.swing.JButton();
        jButtonPlay = new javax.swing.JButton();
        jButtonStop = new javax.swing.JButton();
        jSlider1 = new javax.swing.JSlider();
        jButtonBackward = new javax.swing.JButton();
        jButtonForward = new javax.swing.JButton();
        jEndTimeLabel = new javax.swing.JLabel();
        jCurrentTimeLabel = new javax.swing.JLabel();
        lbCurSpeed = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        jButton4.setText("jButton3");

        setBackground(new java.awt.Color(204, 204, 204));
        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setPreferredSize(new java.awt.Dimension(1000, 180));

        jButtonPlay.setForeground(new java.awt.Color(204, 204, 204));
        jButtonPlay.setBorder(null);
        jButtonPlay.setFocusable(false);
        jButtonPlay.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButtonPlayMouseReleased(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButtonPlayMousePressed(evt);
            }
        });

        jButtonStop.setForeground(new java.awt.Color(240, 240, 240));
        jButtonStop.setBorder(null);
        jButtonStop.setFocusable(false);
        jButtonStop.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButtonStopMousePressed(evt);
            }
        });
        jButtonStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStopActionPerformed(evt);
            }
        });

        jSlider1.setPaintTicks(true);
        jSlider1.setSnapToTicks(true);
        jSlider1.setValue((int)(long)(currTime/totalTime*100));
        jSlider1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 153)));
        jSlider1.setFocusable(false);
        jSlider1.setPreferredSize(new java.awt.Dimension(200, 20));
        jSlider1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jSlider1MouseReleased(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jSlider1MousePressed(evt);
            }
        });

        jButtonBackward.setBorder(null);
        jButtonBackward.setFocusable(false);
        jButtonBackward.setMaximumSize(new java.awt.Dimension(25, 9));
        jButtonBackward.setMinimumSize(new java.awt.Dimension(25, 9));
        jButtonBackward.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButtonBackwardMouseReleased(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButtonBackwardMousePressed(evt);
            }
        });
        jButtonBackward.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBackwardActionPerformed(evt);
            }
        });

        jButtonForward.setBorder(null);
        jButtonForward.setFocusable(false);
        jButtonForward.setMaximumSize(new java.awt.Dimension(25, 9));
        jButtonForward.setMinimumSize(new java.awt.Dimension(25, 9));
        jButtonForward.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButtonForwardMouseReleased(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButtonForwardMousePressed(evt);
            }
        });
        jButtonForward.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonForwardActionPerformed(evt);
            }
        });

        jEndTimeLabel.setText(String.valueOf(totalTime)+" ms");

        jCurrentTimeLabel.setText(String.valueOf(currTime)+" ms");

        lbCurSpeed.setForeground(new java.awt.Color(51, 51, 51));
        lbCurSpeed.setText(String.valueOf(df.format(currSpeed)));

        jLabel1.setText("Speed");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jCurrentTimeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonPlay, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonBackward, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(2, 2, 2)
                        .addComponent(jButtonForward, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonStop, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel1))
                    .addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, 846, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jEndTimeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lbCurSpeed, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(132, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSlider1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCurrentTimeLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jEndTimeLabel, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonPlay, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lbCurSpeed)
                        .addComponent(jLabel1))
                    .addComponent(jButtonBackward, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonForward, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonStop, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
 
    
    private void jButtonPlayMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonPlayMousePressed
        // TODO add your handling code here:
        ImageIcon iconPlay = new ImageIcon(urlPlay);
        ImageIcon iconPause = new ImageIcon(urlPause);
//        ImageIcon grayIcon = new ImageIcon(GrayFilter.createDisabledImage(icon.getImage()));
        
        if(isplay == true)
        {
            jButtonPlay.setIcon(new ImageIcon(GrayFilter.createDisabledImage(iconPause.getImage())));
            
        }else
            jButtonPlay.setIcon(new ImageIcon(GrayFilter.createDisabledImage(iconPlay.getImage())));
    }//GEN-LAST:event_jButtonPlayMousePressed

    private void jButtonPlayMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonPlayMouseReleased
        // TODO add your handling code here:
        ImageIcon iconPlay = new ImageIcon(urlPlay);
     
//        jButtonPlay.setIcon(icon);
        ImageIcon iconPause = new ImageIcon(urlPause);

        boolean b = false;
        if(isplay == true)
        {
            if(container != null)
                b = container.Pause();
            if(b){
                isplay = false;
                jButtonPlay.setIcon(iconPlay);
//                jButtonPlay.setRolloverIcon(iconPlayG);
            }
            else{
                jButtonPlay.setIcon(iconPause);
//                jButtonPlay.setRolloverIcon(iconPauseG);
            }
            
            
        }
        else
        {
            if(container != null)
                b = container.Play();
            if(b){
                isplay = true;
                jButtonPlay.setIcon(iconPause);
//                jButtonPlay.setRolloverIcon(iconPauseG);
            }
            else{
                jButtonPlay.setIcon(iconPlay);
//                jButtonPlay.setRolloverIcon(iconPlayG);
            }
            
            
        }
        System.out.println(isplay == true ? "playing" : "pause");

    }//GEN-LAST:event_jButtonPlayMouseReleased

    private void jButtonBackwardMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonBackwardMousePressed
        // TODO add your handling code here:
        ImageIcon icon = new ImageIcon(urlBack);
        ImageIcon grayIcon = new ImageIcon(GrayFilter.createDisabledImage(icon.getImage()));
        jButtonBackward.setIcon(grayIcon);
    }//GEN-LAST:event_jButtonBackwardMousePressed

    private void jButtonBackwardMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonBackwardMouseReleased
        // TODO add your handling code here:
        ImageIcon icon = new ImageIcon(urlBack);
        jButtonBackward.setIcon(icon);
    }//GEN-LAST:event_jButtonBackwardMouseReleased

    private void jButtonForwardMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonForwardMousePressed
        // TODO add your handling code here:
        ImageIcon icon = new ImageIcon(urlForward);
        ImageIcon grayIcon = new ImageIcon(GrayFilter.createDisabledImage(icon.getImage()));
        jButtonForward.setIcon(grayIcon);
    }//GEN-LAST:event_jButtonForwardMousePressed

    private void jButtonForwardMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonForwardMouseReleased
        // TODO add your handling code here:
        ImageIcon icon = new ImageIcon(urlForward);
        jButtonForward.setIcon(icon);
    }//GEN-LAST:event_jButtonForwardMouseReleased

    private void jButtonStopMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonStopMousePressed
        // TODO add your handling code here:
        ImageIcon icon = new ImageIcon(urlStop);
//        ImageIcon grayIcon = new ImageIcon(GrayFilter.createDisabledImage(icon.getImage()));
        jButtonStop.setIcon(icon);
    }//GEN-LAST:event_jButtonStopMousePressed

    private void jButtonForwardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonForwardActionPerformed
        // TODO add your handling code here:
        if(container.Forward()){
            currSpeed = currSpeed+currSpeed*0.2f;
//        System.out.println(currSpeed);
            lbCurSpeed.setText(String.valueOf(df.format(currSpeed)));
        }
    }//GEN-LAST:event_jButtonForwardActionPerformed

    private void jButtonStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStopActionPerformed
        // TODO add your handling code here:
        if(container != null){
            if(container.Stop()){
                setCurrTime(0);
            }
            jButtonPlay.setIcon(new ImageIcon(urlPlay));
            isplay = false;
        }
    }//GEN-LAST:event_jButtonStopActionPerformed

    private void jButtonBackwardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBackwardActionPerformed
        // TODO add your handling code here:
        if(container.Backward()){
            currSpeed = currSpeed - currSpeed*0.2f;
            lbCurSpeed.setText(String.valueOf(df.format(currSpeed)));
        }
    }//GEN-LAST:event_jButtonBackwardActionPerformed

    private void jSlider1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSlider1MouseReleased
        // TODO add your handling code here:
        javax.swing.JSlider js = (javax.swing.JSlider)evt.getSource();
        System.err.println("choose time:"+js.getValue());
        if(container != null)
            container.UserSetTimePlay(js.getValue());
        isUserSettingSlider = false;
    }//GEN-LAST:event_jSlider1MouseReleased

    private void jSlider1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSlider1MousePressed
        // TODO add your handling code here:
        isUserSettingSlider = true;
    }//GEN-LAST:event_jSlider1MousePressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButtonBackward;
    private javax.swing.JButton jButtonForward;
    private javax.swing.JButton jButtonPlay;
    private javax.swing.JButton jButtonStop;
    private javax.swing.JLabel jCurrentTimeLabel;
    private javax.swing.JLabel jEndTimeLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JLabel lbCurSpeed;
    // End of variables declaration//GEN-END:variables

}
